package com.kudian.restful.service;

import com.kudian.common.persistence.HibernateDao;
import com.kudian.common.persistence.ResultObj;
import com.kudian.common.service.BaseService;
import com.kudian.common.utils.DateUtils;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.dao.*;
import com.kudian.restful.entity.*;
import com.kudian.restful.vo.ad.AdItemVO;
import com.kudian.restful.vo.ad.MidAdRetVO;
import com.kudian.restful.vo.ad.MidAdVO;
import com.kudian.restful.vo.userbonus.*;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.Timestamp;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/18.
 */
@Service
@Transactional(readOnly = true)
public class UserBonusService extends BaseService<UserBonus, Integer> {
    private static final Logger logger = Logger.getLogger(UserBonusService.class);

    @Autowired
    private BonusTypeDao bonusTypeDao;

    @Autowired
    private UserBonusDao userBonusDao;

    @Autowired
    private SellerDao sellerDao;

    @Autowired
    private LoginSessionDao loginSessionDao;

    @Override
    public HibernateDao<UserBonus, Integer> getEntityDao() {
        return userBonusDao;
    }

    public List<BonusTypeItemVO> queryBonustypes(QueryBonusTypeVO vo) {
        List<BonusTypeItemVO> items = null;
        Timestamp d = DateUtils.getSysTimestamp();
        List<BonusType> ls = this.bonusTypeDao.find(Restrictions.eq("sellerId", vo.getSellerId()),
                Restrictions.eq("isDelete", false)
                , Restrictions.ge("sendEndDate", d), Restrictions.le("sendStartDate", d));

        for (BonusType bt : ls) {
            if (items == null) {
                items = new ArrayList<BonusTypeItemVO>();
            }

            BonusTypeItemVO item = new BonusTypeItemVO();

            item.setTypeId(bt.getTypeId());
            item.setTypeName(bt.getTypeName());
            item.setTypeMoney(bt.getTypeMoney());
            item.setUseStartDate(bt.getUseStartDate());
            item.setUseEndDate(bt.getUseEndDate());

            item.setSellerId(bt.getSellerId());
            item.setMinGoodsAmount(bt.getMinGoodsAmount());
            items.add(item);
        }
        return items;
    }

    @Transactional(readOnly = false)
    public ResultObj receive(@RequestBody ReceiveVO vo) {
        ResultObj o = new ResultObj();
        LoginSession ent = loginSessionDao.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
        }

        List<UserBonus> ubonus = userBonusDao.find(Restrictions.eq("userId", ent.getUserId()), Restrictions.isNull("usedTime"));
        if (ubonus != null && !ubonus.isEmpty()) {
            o.setErrcode(0);
            o.setErrmsg("有未使用的同类型优惠卷,不能继续领取!");
            return o;
        }

        //
        BonusType bt = bonusTypeDao.get(vo.getTypeId());
        if (bt == null) {
            o.setErrcode(9999);
            o.setErrmsg("无此类型优惠卷!");
            return o;
        }

        Timestamp n = DateUtils.getSysTimestamp();
        System.out.println("Time=" + n.getTime());
        if (n.getTime() < bt.getSendStartDate().getTime()) {
            o.setErrcode(9999);
            o.setErrmsg("未到领取时间,"+DateUtils.dateFormat(bt.getSendStartDate())+"开始领取");
            return o;
        }

        if (n.getTime() > bt.getSendEndDate().getTime()) {
            o.setErrcode(9999);
            o.setErrmsg("已过领取时间,"+DateUtils.dateFormat(bt.getSendEndDate())+"结束领取");
            return o;
        }
        UserBonus ub = new UserBonus();
        ub.setAddTime(DateUtils.getSysTimestamp());
        // 两个默认值
        ub.setBonusTypeId(0);
        ub.setBonusSn("");
        ub.setUserId(ent.getUserId());
        ub.setTypeId(vo.getTypeId());
        ub.setUsedTime(null);
        ub.setOrderId(0);
        userBonusDao.save(ub);

        o.setErrcode(0);
        o.setErrmsg("成功领取优惠卷");
        return o;
    }


    public QueryBonusRetVO queryByclient(QueryBonusByClient vo) {
        QueryBonusRetVO o = new QueryBonusRetVO();
        LoginSession ent = loginSessionDao.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
        }

        List<UserBonus> ls = userBonusDao.find(Restrictions.eq("userId", ent.getUserId()), Restrictions.isNull("usedTime"));
        for (UserBonus ub : ls) {
            if (o.getItems() == null) {
                o.setItems(new ArrayList<BonusItemVO>());
            }
            BonusItemVO item = new BonusItemVO();
            item.setUserId(ent.getUserId());
            item.setBonusId(ub.getBonusId());
            item.setTypeId(ub.getTypeId());
            item.setAddTime(ub.getAddTime());
            item.setUsedTime(ub.getUsedTime());
            item.setOrderId(ub.getOrderId());

            BonusType bt = bonusTypeDao.get(ub.getTypeId());
            if (bt != null) {
                item.setTypeName(bt.getTypeName());
                item.setTypeMoney(bt.getTypeMoney());
                item.setSendStartDate(bt.getSendStartDate());
                item.setSendEndDate(bt.getSendEndDate());
                item.setUseStartDate(bt.getUseStartDate());
                item.setUseEndDate(bt.getUseEndDate());
                item.setSellerId(bt.getSellerId());
                item.setMinGoodsAmount(bt.getMinGoodsAmount());

                Seller s = sellerDao.get(bt.getSellerId());
                if (s != null) {
                    item.setSellerName(s.getName());
                }
            }
            o.getItems().add(item);
        }

        o.setErrcode(0);
        o.setErrmsg("成功检索优惠卷信息");
        return o;
    }
}
